//
//  Building.m
//
//  Created by TuanNT on 5/4/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Building.h"


NSString *const kBuildingContent = @"Content";
NSString *const kBuildingForm = @"Form";
NSString *const kBuildingLat = @"Lat";
NSString *const kBuildingName = @"Name";
NSString *const kBuildingID = @"ID";
NSString *const kBuildingFeature = @"Feature";
NSString *const kBuildingSub = @"Sub";
NSString *const kBuildingLong = @"Long";
NSString *const kBuildingAddress = @"Address";
NSString *const kBuildingVal = @"Val";
NSString *const kBuildingMain = @"Main";
NSString *const kBuildingSize = @"Size";
NSString *const kBuildingPrice = @"Price";
NSString *const kBuildingImage = @"Image";


@interface Building ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Building

@synthesize content = _content;
@synthesize form = _form;
@synthesize lat = _lat;
@synthesize name = _name;
@synthesize iDProperty = _iDProperty;
@synthesize feature = _feature;
@synthesize sub = _sub;
@synthesize longProperty = _longProperty;
@synthesize address = _address;
@synthesize val = _val;
@synthesize main = _main;
@synthesize size = _size;
@synthesize price = _price;
@synthesize image = _image;


+ (Building *)modelObjectWithDictionary:(NSDictionary *)dict
{
    Building *instance = [[Building alloc] initWithDictionary:dict];
    return instance;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.content = [self objectOrNilForKey:kBuildingContent fromDictionary:dict];
            self.form = [self objectOrNilForKey:kBuildingForm fromDictionary:dict];
            self.lat = [self objectOrNilForKey:kBuildingLat fromDictionary:dict];
            self.name = [self objectOrNilForKey:kBuildingName fromDictionary:dict];
            self.iDProperty = [self objectOrNilForKey:kBuildingID fromDictionary:dict];
            self.feature = [self objectOrNilForKey:kBuildingFeature fromDictionary:dict];
            self.sub = [self objectOrNilForKey:kBuildingSub fromDictionary:dict];
            self.longProperty = [self objectOrNilForKey:kBuildingLong fromDictionary:dict];
            self.address = [self objectOrNilForKey:kBuildingAddress fromDictionary:dict];
            self.val = [self objectOrNilForKey:kBuildingVal fromDictionary:dict];
            self.main = [self objectOrNilForKey:kBuildingMain fromDictionary:dict];
            self.size = [self objectOrNilForKey:kBuildingSize fromDictionary:dict];
            self.price = [self objectOrNilForKey:kBuildingPrice fromDictionary:dict];
            self.image = [self objectOrNilForKey:kBuildingImage fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.content forKey:kBuildingContent];
    [mutableDict setValue:self.form forKey:kBuildingForm];
    [mutableDict setValue:self.lat forKey:kBuildingLat];
    [mutableDict setValue:self.name forKey:kBuildingName];
    [mutableDict setValue:self.iDProperty forKey:kBuildingID];
    [mutableDict setValue:self.feature forKey:kBuildingFeature];
    [mutableDict setValue:self.sub forKey:kBuildingSub];
    [mutableDict setValue:self.longProperty forKey:kBuildingLong];
    [mutableDict setValue:self.address forKey:kBuildingAddress];
    [mutableDict setValue:self.val forKey:kBuildingVal];
    [mutableDict setValue:self.main forKey:kBuildingMain];
    [mutableDict setValue:self.size forKey:kBuildingSize];
    [mutableDict setValue:self.price forKey:kBuildingPrice];
    [mutableDict setValue:self.image forKey:kBuildingImage];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.content = [aDecoder decodeObjectForKey:kBuildingContent];
    self.form = [aDecoder decodeObjectForKey:kBuildingForm];
    self.lat = [aDecoder decodeObjectForKey:kBuildingLat];
    self.name = [aDecoder decodeObjectForKey:kBuildingName];
    self.iDProperty = [aDecoder decodeObjectForKey:kBuildingID];
    self.feature = [aDecoder decodeObjectForKey:kBuildingFeature];
    self.sub = [aDecoder decodeObjectForKey:kBuildingSub];
    self.longProperty = [aDecoder decodeObjectForKey:kBuildingLong];
    self.address = [aDecoder decodeObjectForKey:kBuildingAddress];
    self.val = [aDecoder decodeObjectForKey:kBuildingVal];
    self.main = [aDecoder decodeObjectForKey:kBuildingMain];
    self.size = [aDecoder decodeObjectForKey:kBuildingSize];
    self.price = [aDecoder decodeObjectForKey:kBuildingPrice];
    self.image = [aDecoder decodeObjectForKey:kBuildingImage];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_content forKey:kBuildingContent];
    [aCoder encodeObject:_form forKey:kBuildingForm];
    [aCoder encodeObject:_lat forKey:kBuildingLat];
    [aCoder encodeObject:_name forKey:kBuildingName];
    [aCoder encodeObject:_iDProperty forKey:kBuildingID];
    [aCoder encodeObject:_feature forKey:kBuildingFeature];
    [aCoder encodeObject:_sub forKey:kBuildingSub];
    [aCoder encodeObject:_longProperty forKey:kBuildingLong];
    [aCoder encodeObject:_address forKey:kBuildingAddress];
    [aCoder encodeObject:_val forKey:kBuildingVal];
    [aCoder encodeObject:_main forKey:kBuildingMain];
    [aCoder encodeObject:_size forKey:kBuildingSize];
    [aCoder encodeObject:_price forKey:kBuildingPrice];
    [aCoder encodeObject:_image forKey:kBuildingImage];
}


@end
